// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

import {asyncRoutes, constantRoutes} from '@/router'
import * as roleApi from '@/api/roleApi.js'
import * as Auth from '@/libs/wechat';
import {formatRoutes} from '@/utils/parsing'

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = {...route}
    if (tmp.child) {
      tmp.child = filterAsyncRoutes(tmp.child, roles)
    }
    res.push(tmp)
  })

  return res
}

const state = {
  routes: [],
  addRoutes: [],
  topbarRouters: [],
  sidebarRouters: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    // state.routes = constantRoutes.concat(routes)
    state.routes = routes
  },
  SET_TOPBAR_ROUTES: (state, routes) => {
    state.topbarRouters = routes
  },
  SET_SIDEBAR_ROUTERS: (state, routes) => {
    state.sidebarRouters = routes
  },
}

const actions = {
  generateRoutes({commit}, roleid) {
    return new Promise(async resolve => {
      let accessedRoutes = []
      let menus = []
      // const { rules } = await roleApi.getRoleById(roleid) //本身注释

      //let menusAll = await roleApi.menuListApi() //await异步转同步

      let menusAll = []
      if(roleid.departmentId === 0){
        menusAll = [
          {
            "id": 1,
            "pid": 0,
            "name": "首页",
            "icon": "s-home",
            "component": "/dashboard",
            "perms": "admin:dashboard:list",
            "menuType": "M",
            "url": "",
            "sort": 1,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 2,
            "pid": 0,
            "name": "单位部门管理",
            "icon": "office-building",
            "component": "/department/index",
            "perms": "admin:department:list",
            "menuType": "M",
            "url": "",
            "sort": 2,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 3,
            "pid": 0,
            "name": "通报曝光管理",
            "icon": "menu",
            "component": "/notice/index",
            "perms": "admin:notice:list",
            "menuType": "M",
            "url": "",
            "sort": 3,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 18,
            "pid": 0,
            "name": "领导讲话管理",
            "icon": "mic",
            "component": "/speech/index",
            "perms": "admin:speech:list",
            "menuType": "M",
            "url": "",
            "sort": 3,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 4,
            "pid": 0,
            "name": "重点投资项目管理",
            "icon": "data-analysis",
            "component": "/project/index",
            "perms": "admin:project:list",
            "menuType": "M",
            "url": "",
            "sort": 4,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 6,
            "pid": 0,
            "name": "争取资金情况管理",
            "icon": "s-finance",
            "component": "/fund/index",
            "perms": "admin:fund",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 22,
                "pid": 6,
                "name": "争取资金计划表",
                "icon": "",
                "component": "/fund/plan",
                "perms": "admin:fund:plan:list",
                "menuType": "M",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false,
              },
              {
                "id": 19,
                "pid": 6,
                "name": "争取资金完成情况",
                "icon": "",
                "component": "/fund/process",
                "perms": "admin:fund:process:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              },
            ]
          },
          {
            "id": 7,
            "pid": 0,
            "name": "招商引资任务管理",
            "icon": "set-up",
            "component": "/business/index",
            "perms": "admin:business",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 220,
                "pid": 7,
                "name": "招商引资计划表",
                "icon": "",
                "component": "/business/plan",
                "perms": "admin:business:plan:list",
                "menuType": "M",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false,
              },
              {
                "id": 170,
                "pid": 7,
                "name": "招商引资完成情况",
                "icon": "",
                "component": "/business/process",
                "perms": "admin:business:process:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              },
            ]
          },
          {
            "id": 5,
            "pid": 0,
            "name": "重点工作任务管理",
            "icon": "s-management",
            "component": "/work/index",
            "perms": "admin:work:list",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 8,
            "pid": 0,
            "name": "问卷调查管理",
            "icon": "document-add",
            "component": "/question/index",
            "perms": "admin:question",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 2201,
                "pid": 8,
                "name": "调查问卷设计",
                "icon": "",
                "component": "/question/design",
                "perms": "admin:question:design:list",
                "menuType": "M",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false,
              },
              {
                "id": 1701,
                "pid": 8,
                "name": "用户答案收集",
                "icon": "",
                "component": "/question/answer",
                "perms": "admin:question:answer:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              },
            ]
          },
          {
            "id": 11,
            "pid": 0,
            "name": "意见建议管理",
            "icon": "s-opportunity",
            "component": "/advise/index",
            "perms": "admin:advise:list",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 12,
            "pid": 0,
            "name": "职务管理",
            "icon": "user",
            "component": "/position/index",
            "perms": "admin:position:list",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 13,
            "pid": 0,
            "name": "督查管理",
            "icon": "search",
            "component": "/supervise",
            "perms": "admin:supervise",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 15,
                "pid": 13,
                "name": "我的动态",
                "icon": "",
                "component": "/supervise/report",
                "perms": "admin:supervise:report:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              },
              {
                "id": 14,
                "pid": 13,
                "name": "书记督",
                "icon": "",
                "component": "/supervise/supervision",
                "perms": "admin:supervise:supervision:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              },
            ]
          },
          {
            "id": 16,
            "pid": 0,
            "name": "系统设置",
            "icon": "setting",
            "component": "/operation",
            "perms": "admin:system",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": [
              /*{
                "id": 22,
                "pid": 10,
                "name": "公用数据配置",
                "icon": "",
                "component": "/operation/dataConfig",
                "perms": "admin:dataConfig",
                "menuType": "M",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false,
              },*/
              {
                "id": 17,
                "pid": 16,
                "name": "管理员列表",
                "icon": "",
                "component": "/operation/adminList",
                "perms": "admin:system:admin:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              },
              /*{
                "id": 12,
                "pid": 10,
                "name": "角色管理",
                "icon": "",
                "component": "/operation/identityManager",
                "perms": "admin:system:role:list",
                "menuType": "C",
                "url": "/api/role/getRoleList",
                "sort": 0,
                "isShow": 1,
                "checked": false
              }*/
            ]
          },
          /*{
            "id": 13,
            "pid": 0,
            "name": "用户管理",
            "icon": "user-solid",
            "component": "/user/index",
            "perms": "admin:user:list",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": []
          },
          {
            "id": 110,
            "pid": 0,
            "name": "消息通知",
            "icon": "bell",
            "component": "/message",
            "perms": "admin:message",
            "menuType": "M",
            "url": "",
            "sort": 11,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 111,
                "pid": 110,
                "name": "消息列表",
                "icon": "",
                "component": "/message/index",
                "perms": "admin:message:list",
                "menuType": "C",
                "url": "/api/message/getMessageList",
                "sort": 0,
                "isShow": 1,
                "checked": false
              }
            ]
          },
          {
            "id": 9,
            "pid": 0,
            "name": "数据管理",
            "icon": "coin",
            "component": "/backups",
            "perms": "admin:backups",
            "menuType": "M",
            "url": "",
            "sort": 12,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 70,
                "pid": 9,
                "name": "备份列表",
                "icon": "",
                "component": "/backups/index",
                "perms": "admin:backups:list",
                "menuType": "C",
                "url": "/api/system/backupsList",
                "sort": 0,
                "isShow": 1,
                "checked": false
              }
            ]
          },
          {
            "id": 79,
            "pid": 0,
            "name": "维护",
            "icon": "s-open",
            "component": "/maintain/picture",
            "perms": "admin:maintain",
            "menuType": "M",
            "url": "",
            "sort": 14,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 80,
                "pid": 79,
                "name": "文件库",
                "icon": "",
                "component": "/maintain/picture",
                "perms": "admin:system:attachment:list",
                "menuType": "C",
                "url": "/api/getAttachmentList",
                "sort": 0,
                "isShow": 1,
                "checked": false
              }
            ]
          }*/
        ]
      }else{
        menusAll = [
          {
            "id": 13,
            "pid": 0,
            "name": "督查管理",
            "icon": "search",
            "component": "/supervise",
            "perms": "admin:supervise",
            "menuType": "M",
            "url": "",
            "sort": 5,
            "isShow": 1,
            "checked": false,
            "childList": [
              {
                "id": 15,
                "pid": 13,
                "name": "我的动态",
                "icon": "",
                "component": "/supervise/report",
                "perms": "admin:supervise:report:list",
                "menuType": "C",
                "url": "",
                "sort": 0,
                "isShow": 1,
                "checked": false
              }
            ]
          },
        ]
      }

      menusAll = formatRoutes(menusAll)

      !Auth.isPhone() ? menus = menusAll.filter(item => item.url !== '/javaMobile') : menus = menusAll.filter(item => item.url === '/javaMobile')
      const _routerResult = comRouter(menus, asyncRoutes)
      accessedRoutes = filterAsyncRoutes(_routerResult)
      // todo 这里控制是否过滤路由，经测试有些菜单不能予以设置，比如系统设置等等
      this.state.settings.showSettings = false
      commit('SET_ROUTES', menus)
      commit('SET_TOPBAR_ROUTES', menus)
      if (this.state.settings.topNav) {
        commit('SET_SIDEBAR_ROUTERS', state.sidebarRouters.length ? state.sidebarRouters : menus[0].child)
      } else {
        commit('SET_SIDEBAR_ROUTERS', menus)
      }
      // resolve(menus)
      // commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)

      // commit('SET_ROUTES', asyncRoutes)
      // resolve(asyncRoutes)
    })
  }
}

function comRouter(menus, asyncRouter, hasLeft) {
  const res = []
  asyncRouter.forEach(router => {
    const _leftUrl = hasLeft ? (hasLeft + '/' + router.path) : router.path
    const _hasPromise = menus.filter(item => item.url.startsWith(_leftUrl))
    if (_hasPromise.length > 0) {
      res.push(router)
    }
    if (router.children) {
      comRouter(menus, router.children, router.path)
    }
  })
  return res
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
